#include "../tools.h"


class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        int size = matrix[0].size() * matrix.size();
        int l = 0, r = matrix[0].size() - 1, u = 0, d = matrix.size() - 1;
        int p = 0;
        vector<int> res(size);
        while(p < size){
            for(int i = l; i <= r; ++i){ res[p++] = matrix[l][i];}
            if(p == size) break;
            ++u;
            for(int i = u; i <= d; ++i){ res[p++] = matrix[i][r];}
            if(p == size) break;
            --r;
            for(int i = r; i >= l; --i){ res[p++] = matrix[d][i];}
            if(p == size) break;
            --d;
            for(int i = d; i >= u; --i){ res[p++] = matrix[i][l];}
            if(p == size) break;
            ++l;
        }
        return res;
    }
};



int main(int argc, char const *argv[])
{
    Solution sol;

    vector<vector<int>> data = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
    auto res = sol.spiralOrder(data);
    show(res);


    return 0;
}
